import {Toast} from '@/utils/index'

const config = Symbol('config')
const isCompleteURL = Symbol('isCompleteURL')
const requestBefore = Symbol('requestBefore')
const requestAfter = Symbol('requestAfter')

class httpRequest {
 // 默认配置
  [config]: { baseURL?: string } & UniApp.RequestOptions = {
		baseURL: '', //https://www.baidu.com
		url: '',
		header: {
		  'content-type': 'application/json;charset=utf-8', //后端要传入json，@RequestBody才能解析，传入其它类型会出现 415 application/x-www-form-urlencoded
		  'Authorization':null
		},
		method: 'GET',
		timeout: 3000,
		dataType: 'json',
		responseType: 'text'
  }
  
 // 拦截器
   interceptors = {
     request:(func:Fn) => {
       if (func) {
         httpRequest[requestBefore] = func
       } else {
         httpRequest[requestBefore] = (request) => request
       }
     },
     response: (func:Fn) => {
       if (func) {
         httpRequest[requestAfter] = func
       } else {
         httpRequest[requestAfter] = (response) => response
       }
     },
   }
   
   
    //请求之前，是默认配置
     static [requestBefore](config: UniApp.RequestOptions) {
       return config
     }
     // 请求之后，默认配置发生改变的话
     static [requestAfter](response: any) {
       return response
     }
     // 判断url是否完整
     static [isCompleteURL](url: string) {
       return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
     }
	 
	 
	 request(options: UniApp.RequestOptions & { baseURL?: string }) {
	     options.baseURL = options.baseURL || this[config].baseURL
	     options.dataType = options.dataType || this[config].dataType
	     options.url = httpRequest[isCompleteURL](options.url) ? options.url : (options.baseURL + options.url)
	     options.data = options.data
	     options.header = { ...options.header, ...this[config].header } //这里注释，由外部header决定
	     options.method = options.method || this[config].method
		
		 //console.log("777777777777777777777777777",options);	
			
			
	     options = { ...options, ...httpRequest[requestBefore](options) }

	     return new Promise((resolve, reject) => {
				 options.success = function (res) {
				 /* console.warn("请求的options参数",options)
				 console.warn("服务器请求返回~",res); */ 
				 resolve(httpRequest[requestAfter](res))
	       }
	       options.fail = function (err) {
				 console.warn("v_v 服务器开小差了~",err);
				 Toast("v_v 服务器开小差了~");
				 reject(httpRequest[requestAfter](err))
	       }
		   
		  
	       uni.request(options)
	       // 中断请求实现方法
	       // let obj: any = {}
	       // obj[request.url] = uni.request(options)
	       // abortRequest() {
	       //   for (const key in obj) {
	       //     if (Object.prototype.hasOwnProperty.call(obj, key)) {
	       //       const element = obj[key];
	       //       element.abort()
	       //     }
	       //   }
	       // }
	     })
	   }
	   
	   reqRequest(url: string, data: any = {}, options: Recordable = {}){
		   return this.request({ ...options, url, data, method: options.method?options.method:"GET"})
	   }
	   
	  get(url: string, data: any = {}, options: Recordable = {}) {
		return this.request({ ...options, url, data, method: 'GET' })
	  }
	
	  post(url: string, data: any = {}, options: Recordable = {}) {
		return this.request({ ...options, url, data, method: 'POST' })
	  }

	  put(url: string, data: any = {}, options: Recordable = {}) {
		return this.request({ ...options, url, data, method: 'PUT' })
	  }

	  delete(url: string, data: any = {}, options: Recordable = {}) {
		return this.request({ ...options, url, data, method: 'DELETE' })
	  }

	  getConfig() {
		return this[config]
	  }
	  
	  // 修改默认配置的一个方法，可以修改请求地址，请求方式等等..
	  setConfig(func:Fn) {
		this[config] = func(this[config])
	  }  
	   
   
} //class End

export default httpRequest